package com.ruoyi.server.websocket;


import cn.hutool.json.JSONUtil;

import com.ruoyi.server.api.readcard.entity.Card;
import org.springframework.stereotype.Component;

import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @ClassName WebSocket
 * @Description TODO
 * @Author 17179
 * @Date 2024/6/5 9:46
 **/
@ServerEndpoint(value = "/wsReadCard/{venueUniqueIdentifier}")
@Component
public class ReadCardWebSocket {
    private static ConcurrentHashMap<String, ReadCardWebSocket> webSocketMap = new ConcurrentHashMap<>();
    //实例一个session，这个session是websocket的session
    private Session session;

    //新增一个方法用于主动向客户端发送消息
    public static void sendMessage(Object message, String venueUniqueIdentifier) {
        ReadCardWebSocket webSocket = webSocketMap.get(venueUniqueIdentifier);
        if (webSocket != null) {
            try {
                webSocket.session.getBasicRemote().sendText(JSONUtil.toJsonStr(message));
                System.out.println("【websocket消息】发送消息成功,用户="+venueUniqueIdentifier+",消息内容"+message.toString());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 向前端入场页面发送读卡信息
     * @param card
     */
    public static void sendMessage(Card card) {
        ReadCardWebSocket webSocket = webSocketMap.get(card.getVenueUniqueIdentifier());
        if (webSocket != null) {
            try {

                webSocket.session.getBasicRemote().sendText(JSONUtil.toJsonStr(card));
                //System.out.println("【websocket消息】发送消息成功,用户="+userId+",消息内容"+message.toString());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    public static ConcurrentHashMap<String, ReadCardWebSocket> getWebSocketMap() {
        return webSocketMap;
    }

    public static void setWebSocketMap(ConcurrentHashMap<String, ReadCardWebSocket> webSocketMap) {
        ReadCardWebSocket.webSocketMap = webSocketMap;
    }

    //前端请求时一个websocket时
    @OnOpen
    public void onOpen(Session session, @PathParam("venueUniqueIdentifier") String venueUniqueIdentifier) {
        this.session = session;
        webSocketMap.put(venueUniqueIdentifier, this);
       // sendMessage("CONNECT_SUCCESS", userId);
       // System.out.println("【websocket消息】有新的连接,连接id"+userId);
    }

    //前端关闭时一个websocket时
    @OnClose
    public void onClose(@PathParam("venueUniqueIdentifier") String venueUniqueIdentifier) {
        webSocketMap.remove(venueUniqueIdentifier);
       // System.out.println("【websocket消息】连接断开,总数:"+ webSocketMap.size());
    }

    /**
     * 单项通讯只想前端发送读卡信息
     * @param message
     */
    //前端向后端发送消息
    @OnMessage
    public void onMessage(String message) {
//        if (!message.equals("ping")) {
//            System.out.println("【websocket消息】收到客户端发来的消息:"+message);
//        }
    }
}
